<?php defined('SYSPATH') or die('No direct script access.');

class Model_Project extends Model_Entity
{
	public function get_project_info($project_id)
	{
		return DB::select('project.*', array(DB::select('name')->from('category')->where('id','=',DB::expr('project.category_id')), 'category_name'))
			->from('project')
			->where('id','=',$project_id)->execute()->current();	
	}
	public function get_all_project($user_id)
	{
		$projects = DB::select('project.*', array(DB::select('name')->from('category')->where('id','=',DB::expr('project.category_id')), 'category_name'))
			->from('project')
			->where('user_id','=',$user_id)->execute()->as_array();
		return $this->port_id_2_attribute($projects);
	}
	public function port_id_2_attribute($projects)
	{
		foreach ($projects as $id => $project)
		{
			//$project['type'] 	 = Helper_MyConst::$PROJECT_TYPE_TABLE[$project['type']];
			$project['priority'] = Helper_MyConst::$PROJECT_PRIORITY_TABLE[$project['priority']];
			//$project['status'] 	 = Helper_MyConst::$PROJECT_STATUS_TABLE[$project['status']];
			//$project['scale']    = Helper_MyConst::$PROJECT_SCALE_TABLE[$project['scale']];
			$projects[$id] =  $project;
		}
		return $projects;
	}
	public function add_project($user_id, $info)
	{
		$user_info = Model::factory('user')->get_user_info($user_id);
		$info['status']				= Helper_MyConst::STATUS_IN_PROGRESS;
		$info['date_create']		= $info['date_update'] = DB::expr('NOW()');
		$info['stat_employee']		= 1;
		$info['user_id']			= $user_id;
		$info['user_displayname']	= $user_info['displayname'];
		$info['user_profile_image'] = $user_info['profile_image'];
		//add project
		//echo Debug::dump($info);die();
		
		$ret = $this->add('project', $info);
		//add user as manager
		$info = array(
			'project_id'	=> $ret[0],
			'user_id'		=> $user_id,
			'role'			=> Helper_MyConst::PROJECT_MANAGER,
			'is_removed'	=> Helper_MyConst::PROJECT_ACCESS
		);
		$this->add('project_user', $info);
		//update user's stat_project
		Model::factory('user')->edit_user_info($user_id, array('stat_project' => DB::expr('stat_project+1'))); 
		return $ret;
	}
	public function add_user_2_project($role, $user_id, $project_id)
	{
		$info = array(
			'role' 		=> $role,
			'user_id'	=> $user_id,
			'project_id'=> $project_id,
			'is_removed'=> Helper_MyConst::PROJECT_ACCESS
		);
		$ret = $this->add('project_user', $info);
		if ($ret)
			Model::factory('user')->edit_user_info($user_id, array('stat_project' => DB::expr('stat_project+1'))); 
		return $ret;
	}
	public function remove_user_from_project($user_id, $project_id)
	{
		$ret = DB::delete('project_user')->where('user_id', '=', $user_id)->and_where('project_id','=',$project_id)
			->execute();
		if ($ret)	
			Model::factory('user')->edit_user_info($user_id, array('stat_project' => DB::expr('stat_project-1'))); 
		return $ret;
	}
	public function edit_project_info($project_id, $info)
	{
		return DB::update('project')->set($info)->execute();
	}
	public function get_project_users($project_id)
	{
		$query = DB::select('user.*','project_user.*',
			array(DB::select(DB::expr('count(*)'))->from('task_project_user')->where('assign_user_id','=', 'user.id'),'total_task'))
		->from('project_user')->join('user')->on('project_user.user_id','=','user.id')
			->where('project_user.project_id','=',$project_id);
		return $query->execute()->as_array();
	}
	
	public function get_project_group($project_id, $k)
	{
		
	}
	//////////////////////////////////////////////////////////
	///Category
	/////////////////////////////////////////////////////////
	public function get_suggest_category($term)
	{
		$category = DB::select()->from('category')->where('name','like',DB::expr("'%".$term."%'"))->execute()->as_array();
		foreach($category as $id=>$c)
		{
			$category[$id]['label'] = $c['name'];
			$category[$id]['value'] = $c['name'];
		}
		return $category;
	}
	public function get_category_by_name($name)
	{
		$query = DB::select()->from('category')->where('name','=', $name);
		return $query->execute()->current();
		
	}
	public function add_category($user_id, $name)
	{
		$query = DB::insert('category', array('name', 'user_id', 'date_create'))
				->values(array($name, $user_id, DB::expr('NOW()')));
		return $query->execute();				
	}
}